package com.yuezhi.mylauncher.activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuezhi.mylauncher.R;
import com.yuezhi.mylauncher.base.GeneralParameter;
import com.yuezhi.mylauncher.utils.AdbUtils;
import com.yuezhi.mylauncher.utils.AppUtils;
import com.yuezhi.mylauncher.utils.ToastUtil;
import com.yuezhi.mylauncher.utils.YXApiUtils;

public class MainActivity extends AppCompatActivity {

    private final String TARGET_PACKAGE_NAME = "com.yuezhi.bancard";
    private final StartActivityHandler startActivityHandler = new StartActivityHandler(Looper.getMainLooper());
    private final TimeHandler timeHandler = new TimeHandler(Looper.getMainLooper());
    private TextView tvTipsContent;
    private View viewTipsArrow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppUtils.fullScreen(MainActivity.this);
        setContentView(R.layout.activity_main);
        AppUtils.init(MainActivity.this);
        YXApiUtils.init();

        TextView tvTips = findViewById(R.id.tv_tips);
        tvTipsContent = findViewById(R.id.tv_tips_content);
        viewTipsArrow = findViewById(R.id.view_tips_arrow);
        tvTips.setOnClickListener(view -> {
            if (tvTipsContent.isShown()) {
                tvTipsContent.setVisibility(View.INVISIBLE);
                viewTipsArrow.setVisibility(View.INVISIBLE);
            } else {
                timeHandler.removeCallbacksAndMessages(null);
                timeHandler.sendEmptyMessageDelayed(0, 5000);
                tvTipsContent.setVisibility(View.VISIBLE);
                viewTipsArrow.setVisibility(View.VISIBLE);
            }
        });

        View viewQuit = findViewById(R.id.view_quit);
        AppUtils.showSetting(MainActivity.this, viewQuit);

        TextView tvVersion = findViewById(R.id.tv_version);
        tvVersion.setText(String.format("Ver.%s", TextUtils.isEmpty(AppUtils.getVersionName(MainActivity.this)) ? "unKnown" : AppUtils.getVersionName(MainActivity.this)));

        LinearLayout llAppDetails = findViewById(R.id.ll_app_details);
        TextView tvStart = findViewById(R.id.tv_start);
        ImageView ivLogo = findViewById(R.id.iv_logo);
        TextView tvAppName = findViewById(R.id.tv_app_name);
        PackageManager packageManager = MainActivity.this.getPackageManager();
        if (packageManager != null) {
            try {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(TARGET_PACKAGE_NAME, PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    llAppDetails.setVisibility(View.VISIBLE);
                    tvStart.setVisibility(View.GONE);
                    String title = packageManager.getApplicationLabel(applicationInfo).toString();
                    Drawable icon = packageManager.getApplicationIcon(applicationInfo);
                    tvAppName.setText(TextUtils.isEmpty(title) ? "班牌" : title);
                    ivLogo.setImageDrawable(icon);
                } else {
                    llAppDetails.setVisibility(View.GONE);
                    tvStart.setVisibility(View.VISIBLE);
                }
            } catch (PackageManager.NameNotFoundException e) {
                llAppDetails.setVisibility(View.GONE);
                tvStart.setVisibility(View.VISIBLE);
            }
        } else {
            llAppDetails.setVisibility(View.GONE);
            tvStart.setVisibility(View.VISIBLE);
        }

        llAppDetails.setOnClickListener(view -> {
            startActivityHandler.removeCallbacksAndMessages(null);
            startActivityHandler.sendEmptyMessage(0);
        });
        tvStart.setOnClickListener(view -> {
            startActivityHandler.removeCallbacksAndMessages(null);
            startActivityHandler.sendEmptyMessage(0);
        });

        String launcherPackageName;
        String phoneModel = AppUtils.getPhoneModel();
        if (!TextUtils.isEmpty(phoneModel)) {
            if (phoneModel.equals(GeneralParameter.SMT_RK3288)) {
                launcherPackageName = "com.hdeva.launcher";
            } else {//其他班牌
                launcherPackageName = "com.android.launcher3";
            }
        } else {
            launcherPackageName = "com.android.launcher3";
        }
        AdbUtils.runRootCmd(String.format("pm disable-user %s",launcherPackageName));

        YXApiUtils.init().hideNavBar(true);
        YXApiUtils.init().setSlideShowNotificationBar(false);
        YXApiUtils.init().setSlideShowNavBar(false);

        if (startActivityHandler != null) {
            startActivityHandler.removeCallbacksAndMessages(null);
            startActivityHandler.sendEmptyMessageDelayed(0, 5000);
        }
    }

    private class StartActivityHandler extends Handler {
        public StartActivityHandler(@NonNull Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (AppUtils.isAppInstall(TARGET_PACKAGE_NAME)) {
                    AppUtils.startApps(MainActivity.this, TARGET_PACKAGE_NAME);
                }else {
                    ToastUtil.showMessage("程序还未安装！");
                    startActivityHandler.sendEmptyMessageDelayed(0,60000);
                }
            }
        }
    }

    private class TimeHandler extends Handler {
        public TimeHandler(@NonNull Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (tvTipsContent != null && tvTipsContent.isShown()) {
                    tvTipsContent.setVisibility(View.INVISIBLE);
                    viewTipsArrow.setVisibility(View.INVISIBLE);
                }
            }
        }
    }
}